home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Reference Guide / C-C++ Interactive Reference Guide.iso / c_ref / csource4 / 235_01 / ovdef.c < prev    next >
Text File  |  1987-06-18  |  14KB  |  413 lines

  1. /*  010  14-Feb-87  ovdef.c
  2.  
  3.         Copyright (c) 1987 by Blue Sky Software.  All rights reserved.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include "ov.h"
  8.  
  9. #ifndef NULL
  10. #define NULL (0)
  11. #endif
  12.  
  13. #define FIRST_AROW (3)         /* attribute display */
  14. #define FIRST_ACOL (30)
  15. #define FIRST_TROW (3)         /* text types */
  16. #define FIRST_TCOL (2)
  17. #define FIRST_IROW (14)        /* instructions/directions */
  18. #define FIRST_ICOL (2)
  19.  
  20. #define V_bar (0xba)
  21. #define H_bar (0xcd)
  22.  
  23. extern unsigned char vid_mode;         /* need to know video mode */
  24. extern unsigned char vid_snow;         /* NZ when checking for snow */
  25. extern unsigned char vid_attrib;       /* we hack directly with video attrib */
  26. extern unsigned char def_display;      /* NZ when define screen displayed */
  27. extern unsigned char attribs[], cgaattr[], monattr[]; /* arrays of attrib's */
  28.  
  29. extern char *ovname;                   /* might be name of OV file */
  30. extern char *cantopen;                 /* common text message */
  31. extern char patchstr[];                /* id's patch area in EXE file */
  32.  
  33. extern struct menu_selection top_file_menu[], *top_menu;
  34. extern char *quit_txt;
  35.  
  36. int def_color(), def_snow(), def_exit();
  37. int def_set(), def_noset(), def_reset(), def_write();
  38.  
  39. static struct menu_selection set_attr_menu[] = {
  40.    { "Set", "Set the video attributes to the values shown", def_set, NULL },
  41.    { "Reset", "Reset the video attributes to the default values", def_reset, NULL },
  42.    { "Quit", "Quit without changing the video attributes", def_noset, NULL },
  43.    { NULL, NULL, NULL, NULL }
  44. };
  45.  
  46. static struct menu_selection top_def_menu[] = {
  47.    { "Colors", "Select video display attributes", def_color, set_attr_menu },
  48.    { "Snow", "Enable or disable video \"snow\" checking", def_snow, NULL },
  49.    { "Write", "Make current settings permanent by writing to disk", def_write, NULL },
  50.    { "Quit", "Return to the file display", def_exit, top_file_menu },
  51.    { NULL, NULL, NULL, NULL }
  52. };
  53.  
  54. static struct {                /* define stuff to display the type of */
  55.    char *name;                 /*   parameter to be defined - this table */
  56.    unsigned char row;          /*   must be in DIS_NORM - DIS_TAGD order */
  57.    unsigned char col;
  58. } def_text[] = {
  59.    { "NORMAL TEXT            ", FIRST_TROW,   FIRST_TROW },
  60.    { "HIGHLIGHTED TEXT       ", FIRST_TROW+1, FIRST_TROW },
  61.    { "WINDOW TEXT            ", FIRST_TROW+2, FIRST_TROW },
  62.    { "HIGHLIGHTED WINDOW TEXT", FIRST_TROW+3, FIRST_TROW },
  63.    { "HEADING TEXT           ", FIRST_TROW+4, FIRST_TROW },
  64.    { "BACKGROUND TEXT        ", FIRST_TROW+5, FIRST_TROW },
  65.    { "TAGGED FILE NAME TEXT  ", FIRST_TROW+6, FIRST_TROW } };
  66.  
  67. static char *directions[] = {
  68.      "PgUp & PgDn select the ", "type of text to be set.",
  69.      "                       ", "\x18\x19\x1b\x1a select the colors." };
  70.  
  71. static int text_type;
  72. static unsigned char newattr[7];
  73. static unsigned char defining = FALSE; /* NZ - video attributes being def'd */
  74.  
  75. static char *ovfile = "ov.exe";
  76. static char *nowrit = " - current settings not written";
  77.  
  78. char *getenv(), *strchr();
  79. FILE *fopen(), *pathopen();
  80.  
  81.  
  82. /*****************************************************************************
  83.                                D E F I N E
  84.  *****************************************************************************/
  85.  
  86. define() {             /* allow user to define screen colors / snow checking */
  87.  
  88.    def_display = TRUE;         /* define screen display is active */
  89.  
  90.    strncpy(newattr,attribs,sizeof(newattr));   /* start with current attribs */
  91.  
  92.    def_init();                 /* actually setup the display */
  93.  
  94.    top_menu = top_def_menu;    /* make define menu THE menu */
  95. }
  96.  
  97.  
  98. /*****************************************************************************
  99.                             D E F _ E X I T
  100.  *****************************************************************************/
  101.  
  102. static int
  103. def_exit() {           /* leave define screen parameters mode */
  104.  
  105.    def_display = FALSE;                /* going back to normal */
  106.  
  107.    top_menu = top_file_menu;           /* file menu again */
  108.  
  109.    setup_file_scr();
  110.    update_header();                    /* rewrite the entirey screen */
  111.    refresh_screen(0);                  /* to use possible new colors */
  112. }
  113.  
  114.  
  115. /*****************************************************************************
  116.                             D E F _ I N I T
  117.  *****************************************************************************/
  118.  
  119. static int
  120. def_init() {           /* initialize the define screen */
  121.  
  122.    int i, fg;
  123.    register int bg;
  124.  
  125.    /* initialize the screen image */
  126.  
  127.    setvattrib(DIS_NORM);
  128.  
  129.    clr_scr();
  130.    center_text(0,"DEFINE SCREEN PARAMETERS");
  131.  
  132.    disp_status();                              /* status line */
  133.  
  134.    setvattrib(DIS_BOX);                        /* box to enclose text types */
  135.    popup(FIRST_TROW-1,FIRST_TCOL-1,9,27,NULL);
  136.  
  137.    for (i = DIS_NORM; i <= DIS_TAGD; i++)      /* display text types */
  138.       select_t(0,i);
  139.  
  140.    setvattrib(DIS_BOX);                        /* box to enclose pattern */
  141.    popup(FIRST_AROW-1,FIRST_ACOL-1,18,50,NULL);
  142.  
  143.    for (fg = 0; fg < 16; fg++) {               /* display color pattern */
  144.       gotorc(FIRST_AROW+fg,FIRST_ACOL);
  145.       for (bg = 0; bg < 8; bg++) {
  146.          vid_attrib = (bg << 4) | fg;
  147.          disp_str(" TEXT ");
  148.       }
  149.    }
  150.  
  151.    setvattrib(DIS_NORM);       /* back to "Normal" */
  152.  
  153. }
  154.  
  155.  
  156. /*****************************************************************************
  157.                             D E F _ C O L O R
  158.  *****************************************************************************/
  159.  
  160. static int
  161. def_color() {          /* define screen "colors" */
  162.  
  163.    int i;
  164.  
  165.    defining = TRUE;                    /* yes, we are defining attributes */
  166.    select_t(1,text_type = DIS_NORM);   /* highlight normal text first */
  167.    select_a(1,newattr[text_type]);     /* highlight current attr location */
  168.    top_menu = set_attr_menu;           /* so ESC doesn't leave this menu */
  169.  
  170.    setvattrib(DIS_BOX);               /* box for directions/instructions */
  171.    popup(FIRST_IROW-1,FIRST_ICOL-1,6,27,NULL);
  172.    for(i = 0; i < 4; i++)
  173.       disp_str_at(directions[i],FIRST_IROW+i,FIRST_ICOL+1);
  174.    setvattrib(DIS_NORM);
  175. }
  176.  
  177.  
  178. /*****************************************************************************
  179.                             D E F _ x S E T
  180.  *****************************************************************************/
  181.  
  182. static int
  183. def_set() {            /* use the currently displayed video attributes */
  184.  
  185.    strncpy(attribs,newattr,sizeof(newattr));   /* SET the current values */
  186.    do_set();                                   /* rest of stuff */
  187. }
  188.  
  189. static int
  190. def_noset() {          /* back to the prior values */
  191.  
  192.    strncpy(newattr,attribs,sizeof(newattr));   /* back to current */
  193.    do_set();
  194. }
  195.  
  196. static int
  197. def_reset() {          /* reset to overview default */
  198.  
  199.    strncpy(attribs,vid_mode == 7 ? monattr : cgaattr,sizeof(newattr));
  200.    strncpy(newattr,attribs,sizeof(newattr));   /* back to default */
  201.    do_set();
  202. }
  203.  
  204. static int
  205. do_set() {
  206.    defining = FALSE;                           /* not defining anymore */
  207.    def_init();                                 /* redisplay scr with new vals */
  208.    top_menu = top_def_menu;                    /* back to def menu */
  209. }
  210.  
  211.  
  212. /*****************************************************************************
  213.                             D E F _ S N O W
  214.  *****************************************************************************/
  215.  
  216. static int
  217. def_snow() {
  218.  
  219.    int ch;
  220.    char msg[80];
  221.    static char *enable = "ENABLE", *disable = "DISABLE";
  222.  
  223.    strcpy(msg,"\"snow\" checking is currently ");
  224.    strcat(msg,vid_snow ? enable : disable);
  225.    strcat(msg,"D, do you want to ");
  226.    strcat(msg,vid_snow ? disable : enable);
  227.    strcat(msg," it? (y/N): ");
  228.  
  229.    ch = ask(msg);                      /* ask user what to do */
  230.    if (yes(ch))
  231.       vid_snow = vid_snow ? 0 : 1;
  232.  
  233. }
  234.  
  235.  
  236. /*****************************************************************************
  237.                             D E F _ W R I T E
  238.  *****************************************************************************/
  239.  
  240. static int
  241. def_write() {
  242.  
  243.    int i;
  244.    register int ch;
  245.    char *buffer, *sp;
  246.    register FILE *exe;
  247.  
  248.    /* try to open the EXE file somewhere in the PATH - if this is
  249.       DOS 3.x or greater, ovname should already have the full pathname
  250.       of the .EXE file. */
  251.  
  252.    if ((exe = pathopen(ovname ? ovname : ovfile,"r+b")) == NULL) {
  253.       sp = prompt("","Can't locate OV.EXE, enter filename to write to:",
  254.            NULL,0,MAX_PATHLEN);
  255.       if (strlen(sp))
  256.          exe = fopen(sp,"r+b");
  257.    }
  258.  
  259.    if (exe == NULL) {          /* now give up if not open */
  260.       show_error(0,0,3,cantopen,"OV.EXE",nowrit);
  261.       return;
  262.    }
  263.  
  264.    /* try to allocate a large buffer to access the EXE file */
  265.  
  266.    if (buffer = (char *) malloc(4096))
  267.       setvbuf(exe,buffer,_IOFBF,4096);
  268.  
  269.    /******************* EXE file is open *********************/
  270.  
  271.    /* scan the file looking for the patch string id */
  272.  
  273.    while ((ch = fgetc(exe)) != EOF)            /* slow and stupid, but */
  274.       if (ch == *patchstr) {
  275.          i = 0;
  276.          while ((ch = fgetc(exe)) && ch == patchstr[++i])
  277.             ;
  278.          if (patchstr[++i] == '\0')
  279.             break;
  280.       }
  281.  
  282.    if (ch != EOF) {            /* found it if not EOF */
  283.  
  284.       strncpy(vid_mode == 7 ? monattr : cgaattr,attribs,sizeof(newattr));
  285.       fseek(exe,0L,SEEK_CUR);          /* seek so we can write */
  286.       fwrite(&vid_snow,sizeof(vid_snow)+sizeof(newattr)+sizeof(newattr),1,exe);
  287.  
  288.    } else
  289.       show_error(0,0,2,"Can't find parameters in file",nowrit);
  290.  
  291.    fclose(exe);                /* close */
  292.  
  293.    if (buffer)                 /*   and clean up */
  294.       free(buffer);
  295. }
  296.  
  297.  
  298. /*****************************************************************************
  299.                             D E F _ M O V E
  300.  *****************************************************************************/
  301.  
  302. def_move(dir)          /* move the attribute pointer in given direction */
  303. int dir;
  304. {
  305.    register unsigned int fbg, attr = newattr[text_type];
  306.  
  307.    if (!defining)                      /* don't do anything unless video */
  308.       return;                          /*   attributes are being defined */
  309.  
  310.    select_a(0,attr);                   /* always deselect video attribute */
  311.  
  312.    if (dir == PGUP || dir ==PGDN)      /* maybe deselect text type */
  313.       select_t(0,text_type);
  314.  
  315.    switch (dir) {
  316.  
  317.       case UP:                                 /* up arrow of course */
  318.  
  319.          if (fbg = attr & 15)
  320.             attr = (attr & 0x70) | (fbg - 1);
  321.          else
  322.             attr = (attr & 0x70) | 15;         /* wraps to bottom */
  323.          break;
  324.  
  325.       case DOWN:                               /* down arrow */
  326.  
  327.          if ((fbg = attr & 15) < 15)
  328.             attr = (attr & 0x70) | (fbg + 1);
  329.          else
  330.             attr = attr & 0x70;                /* wraps to top */
  331.          break;
  332.  
  333.       case LEFT:                               /* left arrow */
  334.  
  335.          if (fbg = (attr >> 4) & 7)
  336.             attr = ((fbg - 1) << 4) | (attr & 15);
  337.          else
  338.             attr = 0x70 | (attr & 15);         /* wraps to right */
  339.          break;
  340.  
  341.       case RIGHT:                              /* right arrow */
  342.  
  343.          if ((fbg = (attr >> 4) & 7) < 7)
  344.             attr = ((fbg + 1) << 4) | (attr & 15);
  345.          else
  346.             attr = attr & 15;                  /* wraps to left */
  347.          break;
  348.  
  349.       case PGUP:                               /* page up - new text type */
  350.  
  351.          if (text_type)
  352.             --text_type;
  353.          else
  354.             text_type = 6;
  355.          break;
  356.  
  357.       case PGDN:                               /* page dn - new text type */
  358.  
  359.          if (text_type < 6)
  360.             ++text_type;
  361.          else
  362.             text_type = 0;
  363.          break;
  364.    }
  365.  
  366.    if (dir == PGUP || dir ==PGDN)              /* maybe select new text type */
  367.       attr = newattr[text_type];               /* set attribute for new type */
  368.  
  369.    select_a(1,attr);                   /* show new attribute */
  370.    newattr[text_type] = attr;          /* remember what it is */
  371.  
  372.    select_t(1,text_type);              /* display name in new attr */
  373.  
  374.    setvattrib(DIS_NORM);               /* stick with norm for now */
  375. }
  376.  
  377.  
  378. /*****************************************************************************
  379.                             S E L E C T _ A
  380.  *****************************************************************************/
  381.  
  382. static int
  383. select_a(on_off,attr)  /* highlight the current attribute */
  384. register int on_off, attr;
  385. {
  386.    int row, col;
  387.  
  388.    row = FIRST_AROW + (attr & 15);     /* decode attr to row/col */
  389.    col = FIRST_ACOL + (attr >> 4) * 6;
  390.  
  391.    vid_attrib = attr;                  /* gotta use this attribute */
  392.  
  393.    disp_str_at(on_off ? "\x10TEXT\x11" : " TEXT ",row,col); /* (de)select */
  394. }
  395.  
  396.  
  397. /*****************************************************************************
  398.                             S E L E C T _ T
  399.  *****************************************************************************/
  400.  
  401. static int
  402. select_t(on_off,type)  /* highlight the current text type */
  403. register int on_off, type;
  404. {
  405.    vid_attrib = newattr[type];         /* gotta use this attribute */
  406.  
  407.    /* display pointer, name, pointer */
  408.  
  409.    disp_char_at(on_off ? 0x10 : ' ',FIRST_TROW+type,FIRST_TCOL);
  410.    disp_str(def_text[type].name);
  411.    disp_char(on_off ? 0x11 : ' ');
  412. }
  413.